#!powershell
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

# WANT_JSON
# POWERSHELL_COMMON
function Get-TcpConnectionStatus()
{
    [CmdletBinding()]
		param (
			[parameter(Mandatory=$true)]
			[String]$hostName,
			[parameter(Mandatory=$true)]
			[Int32]$port,
			[parameter(Mandatory=$false)]
			[String]$Comment="Check Endpoint"
			)
    PROCESS 
	{
        $msg = [System.String]::Format("Check connection for {0} on {1}", $hostName, $port)
        Write-Verbose $msg
        
        $tcpClient = New-Object System.Net.Sockets.TCPClient

        $checkResult = New-Object PSObject
        $checkResult | Add-Member -type NoteProperty -name HostName -value $hostName        
        $checkResult | Add-Member -type NoteProperty -name Port -value $port
        $checkResult | Add-Member -type NoteProperty -name Status -Value $null
        $checkResult | Add-Member -type NoteProperty -name LocalEndPoint -Value $null
        $checkResult | Add-Member -type NoteProperty -name RemoteEndPoint -Value $null
		$checkResult | Add-Member -type NoteProperty -name Comment -Value $Comment
        #$checkResult | Add-Member -type NoteProperty -name TTL -value -Value 0

        try
        {
            $tcpClient.Connect($hostName,$port)
            $checkResult.Status = $tcpClient.Connected
            $checkResult.LocalEndPoint = $tcpClient.Client.LocalEndPoint
            $checkResult.RemoteEndPoint = $tcpClient.Client.RemoteEndPoint
            #$checkResult.TTL = $tcpClient.Client.Ttl
        }
        catch
        {
            $checkResult.Status = $False
        }
        finally
        {
            
            write-output $checkResult
            $tcpClient.Close()
        }
        $msg = [System.String]::Format("End Check connection for {0} on {1}", $hostName, $port)
        Write-Verbose $msg        
    }
}    



$params = Parse-Args $args $true;

$hostName = Get-Attr $params "hostName" -failifempty $true
$port = Get-Attr $params "port" $false
$comment = Get-Attr $params "comment" $false

if (-not $comment)
{
    $comment = "Check Endpoint connection for '$hostName' on port '$port'"
}

$result = Get-TcpConnectionStatus -hostName $hostName -port $port -Comment $comment

if (-not ($result.Status))
{
    Fail-Json $result $([String]::Format("Unable to contact Host '{0}' on port '{1}'",  $result.HostName, $result.Port))
}


Exit-Json $result;
